<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>插件管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="/static/bootstrap/bootstrap.min.css?v=3.3.7" rel="stylesheet">
    <link href="../../css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="../../css/animate.min.css?v=4.0.0" rel="stylesheet">
    <link href="../../css/style.min.css?v=4.0.0" rel="stylesheet">

    <link href="../../css-user/plugin.css" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="container-fluid topmenu">
        <h2><i class="fa fa-edit"></i> 插件<span class="pluginName"></span>管理</h2>
    </div>
    <div class="container-fluid plugin-edit">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>插件管理</h5>
                        <div class="ibox-tools">
                            <a class="btn btn-primary btn-xs" href="myplugin.html" role="button">
                                返回
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">

        <!-- Nav tabs -->
        <ul class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active">
                <a href="#home" aria-controls="home" role="tab" data-toggle="tab">基本</a>
            </li>
            <li role="presentation">
                <a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">版本管理</a>
            </li>
            <li role="presentation" class="isTab3" style="display: none;">
                <a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">插件发布</a>
            </li>
        </ul>

        <!-- Tab panes -->
        <div class="tab-content">
            <!-- Tab1 Start -->
            <div role="tabpanel" class="tab-pane active" id="home">
                <form class="form-horizontal" id="pluginConfig">
                    <div class="form-group">
                        <label for="name" class="col-sm-1 control-label">
                            插件名称
                            <p class="text-muted">name</p>
                        </label>
                        <div class="col-sm-11">
                            <p class="form-control-static" id="name"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="displayName" class="col-sm-1 control-label">
                            中文名称
                            <p class="text-muted">displayName</p>
                        </label>
                        <div class="col-sm-11">
                            <input type="text" class="form-control" id="displayName" style="width: 20%;" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="description" class="col-sm-1 control-label">
                            插件介绍
                            <p class="text-muted">description</p>
                        </label>
                        <div class="col-sm-11">
                            <textarea class="form-control" rows="3" id="description" style="width: 45%;"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="" class="col-sm-1 control-label">
                            触发词
                            <p class="text-muted">triggerwords</p>
                        </label>
                        <div class="col-sm-11">
                            <h4 id="triggerwords"></h4>
                            <input type="hidden" name="triggerwords" value="">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-1 control-label">
                            系统插件
                            <p class="text-muted">IsSystem</p>
                        </label>
                        <div class="col-sm-11">
                            <div class="btn-group" id="IsSystem" data-toggle="buttons">
                                <label class="btn btn-white btn-sm active">
                                    <input type="radio" name="IsSystem" value="1"> 是
                                </label>
                                <label class="btn btn-white btn-sm">
                                    <input type="radio" name="IsSystem" value="0"> 否
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-1 control-label">
                            自动启动
                            <p class="text-muted">AutoLoader</p>
                        </label>
                        <div class="col-sm-11">
                            <div class="btn-group" id="AutoLoader" data-toggle="buttons">
                                <label class="btn btn-white btn-sm active">
                                    <input type="radio" name="AutoLoader" value="1"> 是
                                </label>
                                <label class="btn btn-white btn-sm">
                                    <input type="radio" name="AutoLoader" value="0"> 否
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-1 control-label">
                            插件状态
                            <p class="text-muted">IsEnable</p>
                        </label>
                        <div class="col-sm-11">
                            <div class="btn-group" id="IsEnable" data-toggle="buttons">
                                <label class="btn btn-white btn-sm active">
                                    <input type="radio" name="IsEnable" value="1"> 是
                                </label>
                                <label class="btn btn-white btn-sm">
                                    <input type="radio" name="IsEnable" value="0"> 否
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-11">
                            <button type="button" class="btn btn-success" id="saveConfig"> 保 存 </button>&emsp;&emsp;
                            <a class="btn btn-default" href="myplugin.html" role="button">返回</a>
                        </div>
                    </div>
                </form>
            </div>
            <!-- tab1 End -->

            <!-- Tab2 Start -->
            <div role="tabpanel" class="tab-pane" id="profile">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label for="version" class="col-sm-1 control-label">
                            本地版本号
                            <p class="text-muted">version</p>
                        </label>
                        <div class="col-sm-11">
                            <p class="form-control-static" id="version"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="version" class="col-sm-1 control-label">
                            远程版本号
                            <p class="text-muted">originVersion</p>
                        </label>
                        <div class="col-sm-11">
                            <p class="form-control-static" id="origin_version"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-11">
                            <button type="button" class="btn btn-success" disabled="disabled" id="isUpdate">无需升级</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-11">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="version" class="col-sm-1 control-label">
                            插件卸载
                            <p class="text-muted">uninstall</p>
                        </label>
                        <div class="col-sm-11">
                            <button type="button" class="btn btn-danger" id="unInstall">卸载插件</button>
                        </div>
                    </div>
                </div>

            </div>
            <!-- tab2 End -->

            <!-- Tab3 Start -->
            <div role="tabpanel" class="tab-pane" id="messages">
                <div class="form-horizontal">
                    <div class="form-group">
                        <label for="version" class="col-sm-1 control-label">
                            开发者账号
                            <p class="text-muted">developer</p>
                        </label>
                        <div class="col-sm-11" id="developer">
                            <a class="btn btn-info btn-sm" href="#" id="loginDevloper" role="button">登录开发者</a>
                            <p class="form-control-static" id="developer_user" style="display:none;"></p>
                            <input type="hidden" value="" id="developer_webuid" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="displayName" class="col-sm-1 control-label">
                            发布版本号
                            <p class="text-muted">version</p>
                        </label>
                        <div class="col-sm-11 version">
                            <input type="text" class="form-control iSrelease" id="release_version"
                                style="width: 20%;" />
                            <span class="text-muted">示例：2.0.8，升级插件的版本号必须大于前一个版本</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="description" class="col-sm-1 control-label">
                            发布说明
                            <p class="text-muted">description</p>
                        </label>
                        <div class="col-sm-11">
                            <textarea class="form-control iSrelease" rows="3" id="release_explain"
                                style="width: 45%;"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="description" class="col-sm-1 control-label">
                            私有插件
                            <p class="text-muted">private</p>
                        </label>
                        <div class="col-sm-11">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" id="release_private"> 勾选发布后只有开发者本人可见和安装
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-11">
                            <button type="button" class="btn btn-success iSrelease" id="StartRelease">发布插件</button>
                        </div>
                    </div>
                </div>

                <div class="form-horizontal updatebox">
                    <h5>历史版本更新记录</h5>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>版本号</th>
                                <th>更新时间</th>
                                <th>更新日志</th>
                                <th class="text-center">发布状态</th>
                                <th class="text-center">审核状态</th>
                            </tr>
                        </thead>
                        <tbody id="uptabbody">

                        </tbody>
                    </table>
                </div>
            </div>
            <!-- tab3 End -->
        </div>

    </div>
</div>
</div>
</div>

    </div>

    <!-- 全局js -->
    <script src="/static/jquery/jquery.min.js?v=2.1.4" type="text/javascript"></script>
    <script src="/static/bootstrap/bootstrap.min.js?v=3.3.7" type="text/javascript"></script>
    <script src="../../js/content.js?v=1.0.0"></script>

    <!-- 自定义全局js -->
    <script src="../../js-user/public.js" type="text/javascript"></script>

    <!-- 自定义js -->
    <script src="../../js-user/develop.js" type="text/javascript"></script>
    <script>
        $(function () {
            var name = $.getUrlParam('name');
            var urlapi = '../../py/plugin_list.py?op=getinfo&name=' + name + '&r=' + Math.random()
            $.ajax({
                type: "POST",
                url: urlapi,
                dataType: 'json',
                success: function (msg) {
                    //console.log( msg );
                    if (msg.code == '0000') {
                        var data = msg.data;

                        $('.pluginName').text('【' + data.name + '】');

                        // Tab 1
                        $('#name').html(data.name);
                        $('#displayName').val(data.displayName);
                        $('#description').text(data.description);

                        var triggerhtml = '';
                        var triggerval = '';
                        if (data.triggerwords.length > 0) {
                            $.each(data.triggerwords, function (i, n) {
                                triggerhtml += '<span class="label label-default">' + n + '</span> ';
                                triggerval += n +'|#|';
                            });
                        } else {
                            triggerhtml = '<span class="notrigger">无触发词</span>';
                        }
                        $('#triggerwords').html(triggerhtml);
                        $('[name="triggerwords"]').val(triggerval);

                        var eqval = 0;
                        if (data.IsSystem) { eqval = 0; } else { eqval = 1; }
                        $('#IsSystem label').eq(eqval).button('toggle');

                        if (data.AutoLoader) { eqval = 0; } else { eqval = 1; }
                        $('#AutoLoader label').eq(eqval).button('toggle');

                        if (data.IsEnable) { eqval = 0; } else { eqval = 1; }
                        $('#IsEnable label').eq(eqval).button('toggle');

                        // Tab 2
                        $('#version').html('V' + data.version);

                        // Tab 3 加载开发者信息
                        if (data['isRelease'] == 0) {
                            $('.isTab3').show();
                            $('.updatebox').hide();
                            $('#origin_version').html('此插件暂未发布！');

                            ajax_developer(data);
                        } else {
                            $('#origin_version').html(data.origin_version);

                            if (data['isUpdate']>0){
                                $('#isUpdate').removeAttr('disabled').text('一键升级');
                            }
                            ajax_developer(data);
                        }
                    }
                }
            });

            // 显示单个插件更新信息
            show_uplist = function () {
                var pluginName = $('#name').text();
                var userid = $('#developer_webuid').val();
                var urlapi = '../../py/plugin_list.py?op=getupinfo&r=' + Math.random();
                var postdata = {
                    name: pluginName,
                    userid: userid
                };

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: postdata,
                    dataType: 'json',
                    success: function (msg) {
                        if (msg.code == '0000') {
                            var data = msg.data;
                            var tabhtml = '';

                            if (data.length > 0) {
                                $.each(data, function (i, n) {
                                    trhtml = '<tr><td>' + n.version + '</td>';
                                    trhtml += '<td>' + n.updatetime + '</td>';
                                    trhtml += '<td>' + n.explain + '</td>';
                                    trhtml += '<td class="text-center">';
                                    if (n.upload == 1) {
                                        trhtml += '<span class="label label-success">正常</span>';
                                    } else {
                                        trhtml += '<span class="label label-danger">失败</span>';
                                    }
                                    trhtml += '</td>';
                                    trhtml += '<td class="text-center">';
                                    if (n.stauts == 1) {
                                        trhtml += '<span class="label label-success">通过</span>';
                                    } else {
                                        trhtml += '<span class="label label-danger">待审核</span>';
                                    }
                                    trhtml += '</td></tr>';
                                    tabhtml += trhtml;
                                });
                            } else {
                                tabhtml = '<tr><td colspan="5" class="text-center">此项目还没有发布信息！</td></tr>';
                            }
                            $('#uptabbody').html(tabhtml);
                        }
                    }
                });
            };

            // 显示开发者账号
            show_developer = function (demo) {
                if (!$.isEmptyObject(demo)) {
                    $('#developer_user').show().html(demo.nickname);
                    $('#developer_webuid').val(demo.webuid);
                    $('#loginDevloper').hide();
                } else {
                    $('#loginDevloper').show();
                    $('#developer_user').hide();
                    $('#developer_webuid').val('');
                }
            };

            // 加载开发者信息
            ajax_developer = function (loaddata) {
                $.ajax_develop(function(data){
                    var demo = {};
                    if (!$.isEmptyObject(data)){
                        demo = {
                            webuid: data.userid,
                            nickname: data.username
                        }
                    }
                    loading = parent.layer.load(1, {shade: [0.1,'#fff']});
                    show_developer(demo);
                    if (loaddata['isRelease'] == 1) {
                        // 插件已发布且开发者相同
                        if (loaddata['developid'] == data.userid) {
                            $('.isTab3 > a').text('发布升级')
                            $('.isTab3').show();
                            show_uplist();
                        } else {
                            $('.isTab3').hide();
                        }
                    }
                    parent.layer.close(loading);
                });
            };

            // 保存插件配置
            $('#saveConfig').click(function(){
                var _this = $(this);
                var $form = $('form#pluginConfig');

                var pluginName   = $form.find('#name').text();
                var displayName  = $form.find('#displayName').val();
                var description  = $form.find('#description').val();
                var IsSystem     = $form.find('#IsSystem .active input').val();
                var AutoLoader   = $form.find('#AutoLoader .active input').val();
                var IsEnable     = $form.find('#IsEnable .active input').val();

                var urlapi = '../../py/plugin_list.py?op=setconfig&r=' + Math.random();
                var postdata = {
                    pluginName: pluginName,
                    displayName: displayName,
                    description: description,
                    IsSystem: IsSystem==1?true:false,
                    AutoLoader: AutoLoader==1?true:false,
                    IsEnable: IsEnable==1?true:false
                };
                postdata = JSON.stringify(postdata);

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: {data: postdata},
                    dataType: 'json',
                    beforeSend: function () {
                        _this.attr({ 'disabled': 'disabled' }).text('正在保存');
                    },
                    success: function (jsonstr) {
                        if (jsonstr.code == '0000') {
                            parent.swal({title: "恭喜您！", text: jsonstr.message, type: "success"}, function(value) {
                                window.location.replace('/admin/pages/plugin/myplugin.html');
                            });
                        } else {
                            parent.swal("", jsonstr.message, "error");
                        }
                        _this.removeAttr('disabled').text('保存');
                    }
                });
            });

            // 登录开发者账号
            $('#loginDevloper').click(function () {
                var _this = $(this);
                _this.attr({ 'disabled': 'disabled' }).text('开始登录…');
                $.login_develop(function(data){
                    var demo = {};
                    if (!$.isEmptyObject(data)){
                        demo = {
                            webuid: data.userid,
                            nickname: data.nickname
                        }
                    }
                    show_developer(demo);
                    _this.removeAttr('disabled').text('登录开发者');
                });
            });

            // 开始发布
            $('#StartRelease').click(function () {
                var _this = $(this);
                var webuid = $('#developer_webuid').val();
                var version = $('#release_version').val();
                var explain = $('#release_explain').val();
                var private = $('#release_private').is(':checked') ? 1 : 0

                var name = $('#name').text();

                if (version.length <= 0) {
                    parent.swal({title: "发布版本号不能为空!", type: "error"}, function(value) {
                        $('#release_version').focus();
                    });
                    return false;
                }
                if (explain.length < 5) {
                    parent.swal({title: "发布说明不能少于5个字!", type: "error"}, function(value) {
                        $('#release_explain').focus();
                    });
                    return false;
                }

                var urlapi = '../../py/plugin_update.py?op=release&r=' + Math.random();
                var postdata = {
                    name: name,
                    webuid: webuid,
                    version: version,
                    explain: explain,
                    private: private
                };

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: postdata,
                    dataType: 'json',
                    beforeSend: function () {
                        _this.attr({ 'disabled': 'disabled' }).text('正在发布……');
                    },
                    success: function (jsonstr) {
                        if (jsonstr.code == '0000') {
                            parent.swal({title: "恭喜您！", text: jsonstr.message, type: "success"}, function(value) {
                                show_uplist();
                            });
                        } else {
                            parent.swal("", jsonstr.message, "error");
                        }
                        _this.removeAttr('disabled').text('发布插件');
                    }
                });
            });

            // 开始升级插件
            $('#isUpdate').click(function(){
                var _this = $(this);
                var name = $('#name').text();
                var origin_version = $('#origin_version').text();
                var urlapi = '../../py/plugin_update.py?op=update&r=' + Math.random();
                var postdata = {
                    name: name,
                    version: origin_version
                };

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: postdata,
                    dataType: 'json',
                    beforeSend: function () {
                        _this.attr({ 'disabled': 'disabled' }).text('开始升级……');
                    },
                    success: function (jsonstr) {
                        if (jsonstr.code == '0000') {
                            parent.swal({title: "恭喜您！", text: jsonstr.message, type: "success"}, function(value) {
                                window.location.reload();
                            });
                        } else {
                            parent.swal("", jsonstr.message, "error");
                        }
                        _this.removeAttr('disabled').text('一键升级');
                    }
                });
            });

            // 卸载插件
            uninstall_plugin = function(thisobj, pluginName){
                var urlapi = '../../py/plugin_update.py?op=uninstall&r=' + Math.random();
                var postdata = {
                    name: pluginName
                };

                $.ajax({
                    type: "POST",
                    url: urlapi,
                    data: postdata,
                    dataType: 'json',
                    beforeSend: function () {
                        thisobj.attr({ 'disabled': 'disabled' }).text('开始卸载……');
                    },
                    success: function (jsonstr) {
                        if (jsonstr.code == '0000') {
                            parent.swal({title: jsonstr.message, type: "success"},function(value){
                                window.location.replace('/admin/pages/plugin/myplugin.html');
                            });
                        } else {
                            parent.swal({title: jsonstr.message, type: "error"},function(value) {
                                thisobj.removeAttr('disabled').text('卸载插件');
                            });
                        }
                    }
                });
            };

            // 卸载插件
            $('#unInstall').click(function(){
                var _this = $(this);
                var name = $('#name').text();
                parent.swal({
                    title:"您确定要卸载此插件吗",
                    text:"卸载后系统将使用此插件功能，请谨慎操作！",
                    type:"warning",
                    showCancelButton:true,
                    confirmButtonColor:"#DD6B55",
                    confirmButtonText:"是的，我要卸载！",
                    cancelButtonText:"让我再考虑一下…",
                    closeOnConfirm:false
                },function(a){
                    if(a){
                        uninstall_plugin(_this, name);
                    }
                    parent.swal.close();
                });
                return;
            });
        });
    </script>
</body>

</html>